package com.archive.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.archive.common.ResponseBean;
import com.archive.common.ResultUtil;
import com.archive.common.ToolsUtils;
import com.archive.dto.PageDTO;
import com.archive.entity.SysExpire;
import com.archive.entity.SysUser;
import com.archive.service.SysExpireService;
import com.archive.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>
 * token过期表 前端控制器
 * </p>
 *
 * @author Janus
 * @since 2023-10-04
 */
@RestController
@RequestMapping("/expire")
public class ExpireController {

    @Autowired
    SysExpireService expireService;

    @Autowired
    SysUserService userService;


    @RequestMapping("getUserByToken")
    public ResponseBean getUserByToken(String token){
        QueryWrapper<SysExpire> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("token",token);
        SysExpire expire = expireService.getOne(queryWrapper);
        SysUser sysUser = userService.getById(expire.getUserId());
        return ResultUtil.successData(sysUser);
    }


    /**
     * 分页列表查询
     * @param queryParam 查询参数
     * @return
     */
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean getList(PageDTO pageDTO, SysExpire queryParam , HttpSession session) {
        QueryWrapper<SysExpire> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(queryParam.getUserId()!=null,"user_id",queryParam.getUserId());
        queryWrapper.eq(queryParam.getToken()!=null,"token",queryParam.getToken());
        queryWrapper.orderBy(true,pageDTO.getIsAsc().equals("asc"), ToolsUtils.camelToUnderline(pageDTO.getOrderByColumn()));
        IPage<SysExpire> indexPage = new Page<>(pageDTO.getPageNum(),pageDTO.getPageSize());
        IPage<SysExpire> listPage = expireService.page(indexPage, queryWrapper);
        //获取分页信息
        Map pageInfo = ToolsUtils.wrapperPageInfo(listPage);
        return ResultUtil.successData(pageInfo);
    }

}
